package com.lanou.wuyou.gift.category;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.CategoryProductBean;

import java.util.List;

/**
 * Created by dllo on 17/3/20.
 */

public class CategoryProductRightAdapter extends BaseAdapter {
    private Context context;
    private CategoryProductRightItemAdapter adapter;
    private CategoryProductBean bean;
    private int selectIndex;

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
        notifyDataSetChanged();
    }

    public CategoryProductRightAdapter(Context context) {
        this.context = context;
    }

    public void setBean(CategoryProductBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (bean == null){
            return 0;
        }
        if (bean.getData() == null) {
            return 0;
        }
        return bean.getData().getCategories() != null && bean.getData().getCategories().size() > 0 ? bean.getData().getCategories().size() : 0 ;

    }

    @Override
    public Object getItem(int position) {
        return bean.getData().getCategories().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RightHolder rightHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category_product_right,parent,false);
            rightHolder = new RightHolder(convertView);
            convertView.setTag(rightHolder);
        }else {
            rightHolder = (RightHolder) convertView.getTag();
        }
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3,LinearLayoutManager.VERTICAL,false){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        rightHolder.recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new CategoryProductRightItemAdapter(context);
        List<CategoryProductBean.DataBean.CategoriesBean.SubcategoriesBean> subcategoriesBeen =
                bean.getData().getCategories().get(position).getSubcategories();
        adapter.setBean(subcategoriesBeen);
        rightHolder.recyclerView.setAdapter(adapter);
        CategoryProductBean.DataBean.CategoriesBean categoriesBean = bean.getData().getCategories().get(position);
        if (categoriesBean != null){
            if (position != 0){
            rightHolder.titleTv.setText(categoriesBean.getName());
            }
        }
        return convertView;
    }


    class RightHolder{
        RecyclerView recyclerView;
        TextView titleTv;
        public RightHolder(View view){
            recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_product_right);
            titleTv = (TextView) view.findViewById(R.id.tv_itme_title);
        }
    }
}
